<?
	class MySQL
	{
		var $CONN   = '';
		var $DBASE  = DB_DATABASE;
		var $USER   = DB_SERVER_USERNAME;
		var $PASS   = DB_SERVER_PASSWORD;
		var $SERVER = DB_SERVER;

		function error($text)
		{
			$no = mysql_errno();
			$msg = mysql_error();
			echo "[$text] ( $no : $msg )<BR>\n";
			exit;
		}

		function init ()
		{
			$user   = $this->USER;
			$pass   = $this->PASS;
			$server = $this->SERVER;
			$dbase  = $this->DBASE;

			//$conn = mysql_pconnect($server, $user, $pass);
			$conn = mysql_connect($server, $user, $pass);

			if(!$conn) {
				$this->error("Connection attempt failed");
			}

			if(!mysql_select_db($dbase, $conn)) {
				$this->error("Dbase Select failed");
			}

			$this->CONN = $conn;
			return true;
		}

		function query ($sql="")
		{
			if(empty($sql)) {
				return false;
			}

			if(empty($this->CONN)) {
				echo "<H2>No connection!</H2>\n";
				return false;
			}

			$conn = $this->CONN;
			$results = mysql_query($sql,$conn);

			if(!$results) {
				echo "<H2>No results!</H2>\n";
				echo mysql_errno().":  ".mysql_error()."<P>";
				return false;
			}
			return $results;
		}

		function first_row($sql){
		  if($result = $this->query($sql)){
            return ($row=mysql_fetch_array($result));
		  }
		  return $result;
		}

		function result_array($sql="")
    {
			if(empty($sql)) {
				return false;
			}

			if(empty($this->CONN)) {
				echo "<H2>No connection!</H2>\n";
				return false;
			}

			$conn = $this->CONN;
			$results = mysql_unbuffered_query($sql,$conn);

			if(!$results) {
				echo "<H2>No results!</H2>\n";
				echo mysql_errno().":  ".mysql_error()."<P>";
				return false;
			}

			$res=array();
			while ($row=mysql_fetch_array($results,MYSQL_NUM)) {
				array_push($res,$row);
			}
			mysql_free_result($results);
			return $res;
		}

		function rows_count($r){
		  return mysql_num_rows($r);
		}

		function select ($sql="", $column="")
		{
			if(empty($sql)) {
				return false;
			}

			if(!eregi("^select", $sql)) {
				echo "<H2>Wrong function silly!</H2>\n";
				return false;
			}

			if(empty($this->CONN)) {
				return false;
			}

			$conn = $this->CONN;
			$results = mysql_query($sql, $conn);
			if( (!$results) or (empty($results)) ) {
				mysql_free_result($results);
				return false;
			}

			$count = 0;
			$data = array();

			while ( $row = mysql_fetch_array($results)) {
				$data[$count] = $row;
				$count++;
			}

			mysql_free_result($results);

			return $data;
		}

		function sql_query_result ($sql="")
		{
			if(empty($sql)) {
				return false;
			}

			if(empty($this->CONN)) {
				return false;
			}

			$conn = $this->CONN;
			$results = mysql_query($sql, $conn);

			if( (!$results) or (empty($results)) ) {
				mysql_free_result($results);
				return false;
			}

			$count = 0;
			$data = array();

			while ( $row = mysql_fetch_array($results)) {
				$data[$count] = $row;
				$count++;
			}

			mysql_free_result($results);

			return $data;
		}

		function insert ($sql="")
		{
			if(empty($sql)) {
				return false;
			}

			if(!eregi("^insert",$sql)) {
				echo "<H2>Wrong function silly!</H2>\n";
				return false;
			}

			if(empty($this->CONN)) {
				echo "<H2>No connection!</H2>\n";
				return false;
			}

			$conn = $this->CONN;
			$results = mysql_query($sql,$conn);

			if(!$results) {
				echo "<H2>No results!</H2>\n";
				echo mysql_errno().":  ".mysql_error()."<P>";
				return false;
			}

			$results = mysql_insert_id();

			return $results;
		}

}

	$db = new MySQL;

  if(!$db->init())
    {
    echo "Can't connect to database<BR>\n";
    exit();
    }

  if(isset($SQL_NAMES))
    {
    $db->query("SET NAMES ".$SQL_NAMES);
    }
  else
    {
      $db->query("SET NAMES UTF8");
    }
?>